// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   *
   *弹框接口
   */

	// 获取商品列表
	async getgoodslistAction(){
		// 获取商品列表
		let page = this.post();
		let pages = page.currentpage;
		let limits = page.pagesize;
		let brandid = page.brandid;
		let keywords = page.keywords;
		let zt = page.zt;
		let orid = this.userinfo.departmentid;

		let where = {
			isdisabled: zt,
			departmentid: orid
		}
		if (brandid*1) where.brandid = brandid;
		if (keywords) where.name = ['like', '%' + keywords + '%'];

		let data = await this.model('or_goods').where(where).page(pages, limits).countSelect();
		return this.json(data)
	}

	// 获取品牌列表接口
	async getbrandlistAction () {
		let list = await this.model('or_brand').where({isdisabled: 0, departmentid: this.userinfo.departmentid}).select()
		return this.json(list)
	}

	// 项目分类
	async getsubjectcategorylistAction () {
		let list = await this.model('or_subjectcategory').where({isdisable: 0, departmentid: this.userinfo.departmentid}).select()
		return this.json(list)
	}
	// 项目列表
	async getsubjectlistAction(){
		// 获取项目列表
		let filter = JSON.parse(this.post('filter'))
		let where = {
			isdisable: filter.isdisabled,
			departmentid: this.userinfo.departmentid
		}
		if (Number(filter.categoryid)) where.categoryid = filter.categoryid
		if (filter.keywords) where.name = ['like', '%' + filter.keywords + '%']
			
		let list = await this.model('or_subject').where(where).page(filter.currentpage, filter.pagesize).select() // 数据
		let count = await this.model('or_subject').where(where).count() // 数量
		let data = {
			list: list,
			count: count
		}
		return this.json(data)
	}

	// 机构厂商列表
	async factorylistAction(){
		let page = this.post();
		let pages = page.pages;
		let limits = page.limits;
		let search = page.search;
		let orid = this.userinfo.departmentid;

		let data = await this.model('or_factory')
				.where({departmentid: orid, isdisabled: 0, "name|remarks":['like','%'+search+'%']})
				.page(pages, limits)
				.order('id DESC')
				.countSelect();

		return this.json(data);
	}

	// 机构分类列表
	async categorylistAction(){
		let shopid = this.userinfo.departmentid;
		let plist = await this.model().query('SELECT * FROM think_or_subjectcategory WHERE departmentid='+shopid+' AND pid=0');
		let clist = await this.model().query('SELECT * FROM think_or_subjectcategory WHERE departmentid='+shopid+' AND pid!=0');
		let data = {
			p: plist,
			c: clist
		}
		return this.json(data);
	}
}